package com.hepinghospital.user.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.rest.response.BaseJsonResponse;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @time 2021/1/7 16:23
 */

public class EyeYaDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<BaseJsonResponse.Impression> mEyeYaList = new ArrayList<>();
    private OnItemClickListener mListener;
    int Position;
    Boolean isFromHome = true;
    private String mCheckFlag;


    public EyeYaDetailAdapter(Context context, List<BaseJsonResponse.Impression> list) {
        this.mEyeYaList = list;

    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from( context )
                .inflate( R.layout.item_eye_ya_detail, parent, false );
        return new ViewHolder( view );

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder mHolder = (ViewHolder) holder;

        mHolder.eyeYaNum.setText( "第" + (position + 1) + "次测量" );
        mEyeYaList.get( position ).title = mHolder.eyeYaNum.getText().toString().trim();
        Log.e( "mCheckFlag---", mCheckFlag );
        if ("2".equals( mCheckFlag )) {//不可编辑
            mHolder.ivDelEyeYa.setVisibility( View.GONE );
            mHolder.etEyeYaLeft.setVisibility( View.GONE );
            mHolder.etEyeYaRight.setVisibility( View.GONE );
            mHolder.tvEyeYaLeft.setVisibility( View.VISIBLE );
            mHolder.tvEyeYaRight.setVisibility( View.VISIBLE );
            mHolder.tvEyeYaLeft.setText( mEyeYaList.get( position ).osEyePressure );
            mHolder.tvEyeYaRight.setText( mEyeYaList.get( position ).odEyePressure );
        } else {
            if (position == 0) {
                mHolder.ivDelEyeYa.setVisibility( View.GONE );
            } else {
                mHolder.ivDelEyeYa.setVisibility( View.VISIBLE );
            }
            mHolder.etEyeYaLeft.setVisibility( View.VISIBLE );
            mHolder.etEyeYaRight.setVisibility( View.VISIBLE );
            mHolder.tvEyeYaLeft.setVisibility( View.GONE );
            mHolder.tvEyeYaRight.setVisibility( View.GONE );
            mHolder.etEyeYaLeft.setText( mEyeYaList.get( position ).osEyePressure );
            mHolder.etEyeYaRight.setText( mEyeYaList.get( position ).odEyePressure );
        }


        mHolder.ivDelEyeYa.setOnClickListener( v -> {
            mEyeYaList.remove( position );
            notifyDataSetChanged();
        } );
        mHolder.etEyeYaLeft.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mEyeYaList.get( position ).osEyePressure = mHolder.etEyeYaLeft.getText().toString().trim();
            }
        } );
        mHolder.etEyeYaRight.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mEyeYaList.get( position ).odEyePressure = mHolder.etEyeYaRight.getText().toString().trim();
            }
        } );


    }

    @Override
    public int getItemCount() {
        return mEyeYaList.size();
    }

    public void setCheckFlag(String checkFlag) {
        this.mCheckFlag = checkFlag;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }


    public interface OnItemClickListener {
        void callback(BaseJsonResponse.Impression impression);

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.eye_ya_num)
        TextView eyeYaNum;
        @BindView(R.id.iv_del_eye_ya)
        ImageView ivDelEyeYa;
        @BindView(R.id.tv_ya_average_left)
        TextView tvYaAverageLeft;
        @BindView(R.id.et_eye_ya_left)
        EditText etEyeYaLeft;
        @BindView(R.id.tv_eye_ya_left)
        TextView tvEyeYaLeft;
        @BindView(R.id.tv_ya_average_right)
        TextView tvYaAverageRight;
        @BindView(R.id.et_eye_ya_right)
        EditText etEyeYaRight;
        @BindView(R.id.tv_eye_ya_right)
        TextView tvEyeYaRight;

        public ViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }
    }

}
